<?php

/**
 * This file holds the library functions for game management.
 * 
 * @package Game_library
 */


require_once("lib/db.lib.php");
require_once("lib/table.lib.php");
require_once("conf/game.conf.php");

/**
 * Add a new game.
 * 
 * @param	int		$player_no		Number of players.
 * 
 * @return	int|FALSE	<b>Game ID</b> on success, <b>FALSE</b> on failure.
 */
function game_add_new($player_no){
	
	db_connect();
	
	
	if(!($player_no >= 3 && $player_no <= 5)){
		return FALSE;
	}
	
	$sql = "INSERT INTO `game_info` (`player_no`) VALUES (\"$player_no\");";
	
	
	$result = mysql_query($sql);
		
	$game_id = mysql_insert_id();	
	
	//return
	if($result){
		return $game_id;
	}
		
	return FALSE;
	
}

/**
 * Associate users to a game.
 * 
 * @param	int		$game_id		Game ID.
 * @param	Array	$user_arr		Array of user ID.
 */
function game_assoc_user($game_id, $user_arr){
	
	db_connect();
	
	foreach($user_arr as $user_id){
		$sql = "INSERT INTO `game_has_user` (`game_id`, `user_id`) VALUES (\"$game_id\", \"$user_id\");";
		mysql_query($sql);
	}
	
}

/**
 * Set the waterlevel card of a turn of a round of a game.
 * 
 * @param	int		$game_id			Game ID.
 * @param	int		$round				Round.
 * @param	int		$turn				Turn.
 * @param	int		$max_waterlevel		Max waterlevel card.
 * @param	int		$min_waterlevel		Min waterlevel card.
 *
 * @return	bool	<b>TRUE</b> if successed, <b>FALSE</b> if failed.
 */
function game_set_waterlevel($game_id, $round, $turn, $max_waterlevel, $min_waterlevel){

	db_connect();
	
	$sql = "INSERT INTO `game_dealt_waterlevel` (`game_id`, `round`, `turn`, `max_waterlevel`, `min_waterlevel`) VALUES (\"$game_id\", \"$round\", \"$turn\", \"$max_waterlevel\", \"$min_waterlevel\");";
	
	$result = mysql_query($sql);	
	
	if($result){
		return TRUE;
	}
		
	return FALSE;
}

/**
 * Set the card of a user per card per round per game.
 * 
 * @param	int		$game_id	Game ID.
 * @param	int		$user_id	User ID.
 * @param	int		$round		Round.
 * @param	int		$card		Card.
 *
 * @return	bool	<b>TRUE</b> if successed, <b>FALSE</b> if failed.
 */
function game_set_player_hand($game_id, $user_id, $round, $card){

	db_connect();
	
	$sql = "INSERT INTO `player_dealt_hand` (`game_id`, `user_id`, `round`, `card`) VALUES (\"$game_id\", \"$user_id\", \"$round\", \"$card\");";
	
	$result = mysql_query($sql);	
	
	if($result){
		return TRUE;
	}
		
	return FALSE;
}


/**
 * Set waterlevel of user per turn.
 * 
 * @param	int		$game_id		Game ID.
 * @param	int		$user_id		User ID.
 * @param	int		$round			Round.
 * @param	int		$turn			Turn.
 * @param	int		$waterlevel		Waterlevel.
 * @param	int		$buoy_remain	Buoy remain.
 *
 * @return	bool	<b>TRUE</b> if successed, <b>FALSE</b> if failed.
 */
function game_set_player_waterlevel($game_id, $user_id, $round, $turn, $waterlevel, $buoy_remain){

	db_connect();
	
	$sql = "INSERT INTO `player_recv_waterlevel` (`game_id`, `user_id`, `round`, `turn`, `waterlevel`, `buoy_remain`) VALUES (\"$game_id\", \"$user_id\", \"$round\", \"$turn\", \"$waterlevel\", \"$buoy_remain\");";
	
	$result = mysql_query($sql);	
	
	if($result){
		return TRUE;
	}
		
	return FALSE;
}


/**
 * Get the number of players in a game;
 * 
 * @param	int		$game_id	Game ID.
 * 
 * @return	int|FALSE	<b>Number of player ID</b> of game, <b>FALSE</b> if failed.
 */
function game_get_num_of_players($game_id){
	db_connect();
	
	$sql = "SELECT `player_no` FROM `game_info` WHERE `game_id` = \"$game_id\";";
	
	$result = mysql_query($sql);
		
	
	//return
	if($row = mysql_fetch_assoc($result)){
		return (int) $row['player_no'];
	}
		
	return FALSE;
}

/**
 * Get the current round in a game;
 * 
 * @param	int		$game_id	Game ID.
 * 
 * @return	int|FALSE	<b>Current round</b> of game, <b>FALSE</b> if failed.
 */
function game_get_current_round($game_id){
	db_connect();
	
	$sql = "SELECT `current_round` FROM `game_info` WHERE `game_id` = \"$game_id\";";
	
	$result = mysql_query($sql);
		
	
	//return
	if($row = mysql_fetch_assoc($result)){
		return (int) $row['current_round'];
	}
		
	return FALSE;
}

/**
 * Get the current turn in a game;
 * 
 * @param	int		$game_id	Game ID.
 * 
 * @return	int|FALSE	<b>Current turn</b> of game, <b>FALSE</b> if failed.
 */
function game_get_current_turn($game_id){
	db_connect();
	
	$sql = "SELECT `current_turn` FROM `game_info` WHERE `game_id` = \"$game_id\";";
	
	$result = mysql_query($sql);
		
	
	//return
	if($row = mysql_fetch_assoc($result)){
		return (int) $row['current_turn'];
	}
		
	return FALSE;
}

/**
 * Get the status of a game;
 * 
 * @param	int		$game_id	Game ID.
 * 
 * @return	int|FALSE	<b>Status</b> of game, <b>FALSE</b> if failed.
 */
function game_get_status($game_id){
	db_connect();
	
	$sql = "SELECT `game_status` FROM `game_info` WHERE `game_id` = \"$game_id\";";
	
	$result = mysql_query($sql);
		
	
	//return
	if($row = mysql_fetch_assoc($result)){
		return (int) $row['game_status'];
	}
		
	return FALSE;
}

/**
 * Get all users in a game.
 * 
 * @param	int		$game_id	Game ID.
 * 
 * @return	Array|FALSE	An array holding the user ID of all users. <b>FALSE</b> if failed.
 */
function game_get_users($game_id){
	
	db_connect();
	
	
	$user_arr = array();
	
	$sql = "SELECT `user_id` FROM `game_has_user` WHERE `game_id` = \"$game_id\";";
	
	$result = mysql_query($sql);
	
	if(!$result){
		return FALSE;
	}
	
	$user_arr = array();
	while($row = mysql_fetch_assoc($result)){
		array_push($user_arr, $row['user_id']);
	}
	
	return $user_arr;
}



/**
 * Generate and set the water level cards of a game.
 * 
 * @param	int		$game_id	Game ID.
 * 
 * @return	bool	<b>TRUE</b> if successed, <b>FALSE</b> if failed.
 */
function game_gen_waterlevel($game_id){

	db_connect();
	
	$num_of_players = game_get_num_of_players($game_id);
	if(!$num_of_players){
		return FALSE;
	}
	
	for($r=1;$r<=$num_of_players;$r++){
		$card_stack = array();
	
		for($i=1;$i<=12;$i++){
			array_push($card_stack, $i);
			array_push($card_stack, $i);
		}
		
		shuffle($card_stack);
	
		for($i=1;$i<=12;$i++){
			$a = array_pop($card_stack);
			$b = array_pop($card_stack);
			
			if($a > $b){
				$max = $a;
				$min = $b;
			}else{
				$min = $a;
				$max = $b;
			}
			
			//echo "$r-$i:   ".$max."   ".$min."\n";
			if(!game_set_waterlevel($game_id, $r, $i, $max, $min)){
				return FALSE;
			}
		}	
		//echo "\n\n";
	}
	
	return TRUE;
}

/**
 * Calculate the total number of buoys of a hand.
 * 
 * @param	Array	$hand	Array of cards in the hand.
 * 
 * @return	int		Total number of buoys.
 */
function game_cal_buoy_num($hand){
	
	global $card_buoy_value;
	
	
	$sum = 0;
	foreach($hand as $key){
		$sum += $card_buoy_value[$key];
	}
	
	return (int) $sum;
}


/**
 * Generate and set the player hand cards of a game.
 * 
 * @param	int		$game_id	Game ID.
 * 
 * @return	bool	<b>TRUE</b> if successed, <b>FALSE</b> if failed.
 */
function game_gen_player_hand($game_id){

	db_connect();
	
	// check if cards already dealt
	$sql = "SELECT `game_id` FROM `player_dealt_hand` WHERE `game_id` = \"$game_id\";";
	$result = mysql_query($sql);
	
	if(mysql_num_rows($result)>0){
		return FALSE;
	}
	
	
	$num_of_players = game_get_num_of_players($game_id);
	$user_arr = game_get_users($game_id);
	
	$card_stack = array();
	
	for($i=1;$i<=60;$i++){
		array_push($card_stack, $i);
	}
	
	shuffle($card_stack);
	
	//init array
	for($j=0;$j<$num_of_players;$j++){
		$hand[$j] = array();
	}
	
	//deal cards
	for($i=1;$i<=12;$i++){
		for($j=0;$j<$num_of_players;$j++){
			array_push($hand[$j], array_pop($card_stack));
		}
	}
	
	//print_r($user_arr);
	
	$shift = 0;
	foreach($user_arr as $user_id){
		for($i=0;$i<$num_of_players;$i++){
			for($j=0;$j<12;$j++){
				//echo "$game_id $user_id  ($i+1) ".$hand[($i + $shift) % $num_of_players][$j]."\n";
				if(!game_set_player_hand($game_id, $user_id, $i+1, $hand[($i + $shift) % $num_of_players][$j])){
					return FALSE;
				}
			}
			
			// set number of buoy
			$buoy_num = game_cal_buoy_num($hand[($i + $shift) % $num_of_players]);			
			game_set_player_waterlevel($game_id, $user_id, $i+1, 0, 0, $buoy_num);
			//echo "\n";
		}
		
		$shift++;
		//echo "<hr>\n";
	}
	
	
	return TRUE;
}

/**
 * Set a game status.
 * 
 * @param	int		$game_id	Game ID
 * @param	int		$status		Status (GAME_STATUS_READY) [1:Ready, 2:Playing, 3:Ended]
 * 
 * @return	bool	<b>TRUE</b> if successed, <b>FALSE</b> if failed.
 */
function game_set_status($game_id, $status){
	db_connect();
	
	$sql = "UPDATE `game_info` SET `game_status` = \"$status\" WHERE `game_id` = \"$game_id\";";
	
	$result = mysql_query($sql);	
	
	if(mysql_affected_rows() > 0){
		return TRUE;
	}
		
	return FALSE;
}



/**
 * Set the current round of a game.
 * 
 * @param	int		$game_id			Game ID
 * @param	int		$current_round		Current round
 * 
 * @return	bool	<b>TRUE</b> if successed, <b>FALSE</b> if failed.
 */
function game_set_current_round($game_id, $current_round){
	db_connect();
	
	$sql = "UPDATE `game_info` SET `current_round` = \"$current_round\" WHERE `game_id` = \"$game_id\";";
	
	$result = mysql_query($sql);	
	
	if(mysql_affected_rows() > 0){
		return TRUE;
	}
		
	return FALSE;
}

/**
 * Set the current turn of a game.
 * 
 * @param	int		$game_id			Game ID
 * @param	int		$current_turn		Current turn
 * 
 * @return	bool	<b>TRUE</b> if successed, <b>FALSE</b> if failed.
 */
function game_set_current_turn($game_id, $current_turn){
	db_connect();
	
	$sql = "UPDATE `game_info` SET `current_turn` = \"$current_turn\" WHERE `game_id` = \"$game_id\";";
	
	$result = mysql_query($sql);	
	
	if(mysql_affected_rows() > 0){
		return TRUE;
	}
		
	return FALSE;
}



/**
 * Make a new game. (Config and set everything)
 * 
 * @param	int		$table_id	Table ID
 * 
 * @return	bool	<b>TRUE</b> if successed, <b>FALSE</b> if failed.
 */
function game_make_new($table_id){
	
	db_connect();
	
	// table already hv game
	if(table_get_game_id($table_id)){
		return FALSE;
	}
	
	$user_arr = table_get_users($table_id);
	$num_of_players = sizeof($user_arr);
	
	if(!($num_of_players >= 3 && $num_of_players <= 5)){
		return FALSE;
	}
	
	//add a new game in DB
	$game_id = game_add_new($num_of_players);
	
	//add user to game
	game_assoc_user($game_id, $user_arr);
	
	//deal waterlevel cards
	if(!game_gen_waterlevel($game_id)){
		return FALSE;
	}
	
	//deal player hand cards
	if(!game_gen_player_hand($game_id)){
		return FALSE;
	}
	
	//add game to table
	if(!table_assoc_game($table_id, $game_id)){
		return FALSE;
	}
	
	//set game status
	if(!game_set_status($game_id, GAME_STATUS_READY)){
		return FALSE;
	}
	
	return TRUE;
	
}

/**
 * Start a new game.
 * 
 * Set current round and turn to GAME_START_ROUND and GAME_START_TURN.
 * Set status to playing.
 * Cannot start a game that has started.
 * 
 * @param	int		$game_id			Game ID
 * @param	int		$current_turn		Current turn
 * 
 * @return	bool	<b>TRUE</b> if successed, <b>FALSE</b> if failed.
 */
function game_start_game($game_id){
	
	if(game_get_status($game_id) != GAME_STATUS_READY){
		return FALSE;
	}
	
	if(!game_set_current_round($game_id, GAME_START_ROUND)){
		return FALSE;
	}
	
	if(!game_set_current_turn($game_id, GAME_START_TURN)){
		return FALSE;
	}
	
	if(!game_set_status($game_id, GAME_STATUS_PLAYING)){
		return FALSE;
	}
	
	return TRUE;
	
}





/**
 * Get the max waterlevel card of a turn.
 * 
 * @param	int		$game_id		Game ID.
 * @param	int		$round			Round.
 * @param	int		$turn			Turn.
 * 
 * @return	int|FALSE	<b>Max waterlevel</b> of game, <b>FALSE</b> if failed.
 */
function game_get_turn_max_waterlevel($game_id, $round, $turn){

	db_connect();
	
	$sql = "SELECT `max_waterlevel` FROM `game_dealt_waterlevel`WHERE `game_id` = \"$game_id\" AND `round` = \"$round\" AND `turn` = \"$turn\";";
	
	$result = mysql_query($sql);
	
	if($row = mysql_fetch_assoc($result)){
		return (int) $row['max_waterlevel'];
	}
		
	return FALSE;

}


/**
 * Get the min waterlevel card of a turn.
 * 
 * @param	int		$game_id		Game ID.
 * @param	int		$round			Round.
 * @param	int		$turn			Turn.
 * 
 * @return	int|FALSE	<b>Min waterlevel</b> of game, <b>FALSE</b> if failed.
 */
function game_get_turn_min_waterlevel($game_id, $round, $turn){

	db_connect();
	
	$sql = "SELECT `min_waterlevel` FROM `game_dealt_waterlevel`WHERE `game_id` = \"$game_id\" AND `round` = \"$round\" AND `turn` = \"$turn\";";
	
	$result = mysql_query($sql);
	
	if($row = mysql_fetch_assoc($result)){
		return (int) $row['min_waterlevel'];
	}
		
	return FALSE;

}

/**
 * Calculate the number of active players in a game.
 * 
 * @param	int		$game_id		Game ID.
 * 
 * @return	int|FALSE	<b>Number of active players</b> of game, <b>FALSE</b> if failed.
 */
function game_num_of_active_player($game_id){
	
	db_connect();
	
	$round = game_get_current_round($game_id);
	
	$sql = "SELECT count(`user_id`) as `active_user_count`FROM `player_recv_waterlevel`WHERE `game_id` = \"$game_id\" AND `round` = \"$round\" AND `waterlevel` > ".GAME_PLAYER_DEAD_WATERLEVEL.";";
	
	$result = mysql_query($sql);
	
	if($row = mysql_fetch_assoc($result)){
		return (int) $row['active_user_count'];
	}
		
	return FALSE;
}

/**
 * Perform end turn action to a game.
 * 
 * Determine if all players have palyed cards, <br>
 * assign corresponding waterlevel cards,<br>
 * minus buoy for highest waterlevel,<br>
 * kill user if no more buoy.<br>
 * proceed to next turn.
 * 
 * @param	int		$game_id		Game ID.
 * 
 * @return	int|FALSE	<b>Min waterlevel</b> of game, <b>FALSE</b> if failed.
 */
function game_end_turn($game_id){
	
	if(game_get_status($game_id) != GAME_STATUS_PLAYING){
		return FALSE;
	}
	
	db_connect();
	
	
	
	//check if can end turn, all players played cards?
	
	$current_turn = game_get_current_turn($game_id);
	$current_round = game_get_current_round($game_id);
	
	$player_num = game_num_of_active_player($game_id);
	
	$sql = "SELECT * FROM `player_dealt_hand` WHERE `game_id` = \"$game_id\" AND `played_turn` = \"$current_turn\" AND `round` = \"$current_round\" ORDER BY `card` DESC; ";
	
	$result = mysql_query($sql);
	
	if(mysql_num_rows($result) < $player_num){
		return FALSE;
	}
	
	//put the cards in array.
	while($row = mysql_fetch_assoc($result)){
		$cards_arr[$row['user_id']] = $row['card'];
	}
	
	
	
	//set all player waterlevel turn to current turn
	if(!player_set_turn($game_id, $current_round, $current_turn)){
		return FALSE;
	}
	
	
	//assign waterlevel to max and max -2 card
	$count = 1;
	foreach($cards_arr as $user_id => $card){
		if($count == 1){
			
			$waterlevel = game_get_turn_min_waterlevel($game_id, $current_round, $current_turn);			
			if(!player_set_waterlevel($game_id, $user_id ,$current_round, $waterlevel)){
				//return FALSE; //since sometimes the same waterlevel may be assigned again. so may false ligitimately here.
			}
		}else if($count == 2){
			
			$waterlevel = game_get_turn_max_waterlevel($game_id, $current_round, $current_turn);			
			if(!player_set_waterlevel($game_id, $user_id ,$current_round, $waterlevel)){
				//return FALSE;
			}
		}else{
			break;
		}
		
		$count ++;
	}
	
		
	//find max waterlevel
	$sql = "SELECT max(waterlevel) as `max_waterlevel` FROM `player_recv_waterlevel` WHERE `game_id` = \"$game_id\" AND `turn` = \"$current_turn\" AND `round` = \"$current_round\"; ";
	
	$result = mysql_query($sql);
	
	$row = mysql_fetch_assoc($result);
	$max_waterlevel = $row['max_waterlevel'];
	
	
	//find max waterlevel user
	$sql = "SELECT `user_id` FROM `player_recv_waterlevel` WHERE `game_id` = \"$game_id\" AND `turn` = \"$current_turn\" AND `round` = \"$current_round\" AND `waterlevel` = $max_waterlevel; ";

	$result = mysql_query($sql);
	
	while($row = mysql_fetch_assoc($result)){
		$user_id = $row['user_id'];
		
		//minus a buoy
		$buoy_remain = player_get_buoy_remain($game_id, $user_id, $current_round);
		$buoy_remain--;
		if(!player_set_buoy_remain($game_id, $user_id, $current_round, $buoy_remain)){
			return FALSE;
		}
		
		//player dead?
		if($buoy_remain < 0){
			if(!player_set_waterlevel($game_id, $user_id ,$current_round, GAME_PLAYER_DEAD_WATERLEVEL)){
				return FALSE;
			}
			
			//set all remaining weather cards to -1
			$sql = "UPDATE `player_dealt_hand` SET `played_turn` = \"".GAME_PLAYER_DEAD_CARD."\" WHERE `game_id` = \"$game_id\" AND `user_id` = \"$user_id\" AND `round` = \"$current_round\" AND isnull(`played_turn`);";
			
			mysql_query($sql);
			
			
		}
	}
	
	//next turn
	if(!game_set_current_turn($game_id, $current_turn + 1)){
		return FALSE;
	}
			
	
	return TRUE;
}



/**
 * Perform end round action to a game.
 * 
 * Use buoy remain to add points<br>
 * min waterlevel receive 1 more point<br>
 * Proceed to next round<br>
 * 
 * @param	int		$game_id		Game ID.
 * @param	bool	$end_now		Override end round checking. (Default: FALSE)
 * 
 * @return	int|FALSE	<b>Min waterlevel</b> of game, <b>FALSE</b> if failed.
 */
function game_end_round($game_id, $end_now = FALSE){
	
	if(game_get_status($game_id) != GAME_STATUS_PLAYING){
		return FALSE;
	}
	
	db_connect();
	
	//assign points
	
	$current_round = game_get_current_round($game_id);
	$current_turn = game_get_current_turn($game_id);
	
	if(!$end_now){
		if($current_turn <= GAME_ROUND_MAX_TURN){
			return FALSE;
		}
	}
	
	$sql = "SELECT `user_id`, `buoy_remain` FROM `player_recv_waterlevel` WHERE `game_id` = \"$game_id\" AND `round` = \"$current_round\"; ";
	
	$result = mysql_query($sql);
	
	while($row = mysql_fetch_assoc($result)){
		$sql = "UPDATE `game_has_user` SET `game_points` = `game_points` + ${row['buoy_remain']} WHERE `user_id` = \"${row['user_id']}\" AND `game_id` = \"$game_id\";";	
		
		if(!mysql_query($sql)){
			return FALSE;
		}
	}
	
	
	//find min waterlevel and and 1 point
	/*
	$sql = "SELECT `user_id` FROM `player_recv_waterlevel` WHERE `game_id` = \"$game_id\" AND `round` = \"$current_round\" AND `waterlevel` > ".GAME_PLAYER_DEAD_WATERLEVEL." ORDER BY `waterlevel`; ";
	$result = mysql_query($sql);
	
	$row = mysql_fetch_assoc($result);
	
	$user_id =  $row['user_id'];
	
	$sql = "UPDATE `game_has_user` SET `game_points` = `game_points` + 1 WHERE `user_id` = \"${row['user_id']}\" AND `game_id` = \"$game_id\";";	
	
	if(!mysql_query($sql)){
		return FALSE;
	}
	*/
	$sql = "SELECT min(waterlevel) as `min_waterlevel` FROM `player_recv_waterlevel` WHERE `game_id` = \"$game_id\" AND `round` = \"$current_round\" AND `waterlevel` > ".GAME_PLAYER_DEAD_WATERLEVEL." ORDER BY `waterlevel`; ";
	
	$result = mysql_query($sql);
	
	$row = mysql_fetch_assoc($result);
	$min_waterlevel = $row['min_waterlevel'];
	
		//find min waterlevel user
	$sql = "SELECT `user_id` FROM `player_recv_waterlevel` WHERE `game_id` = \"$game_id\" AND `waterlevel` > ".GAME_PLAYER_DEAD_WATERLEVEL." AND `round` = \"$current_round\" AND `waterlevel` = $min_waterlevel; ";
	
	$result = mysql_query($sql);
	if(!$result){
		return FALSE;
	}
	while($row = mysql_fetch_assoc($result)){
		$user_id = $row['user_id'];
		
		$sql = "UPDATE `game_has_user` SET `game_points` = `game_points` + 1 WHERE `user_id` = \"${row['user_id']}\" AND `game_id` = \"$game_id\";";	
	
		if(!mysql_query($sql)){
			return FALSE;
		}
	}
	
	
	
	//add round, reset turn
	
	if(!game_set_current_round($game_id, $current_round + 1)){
		return FALSE;
	}
	
	if(!game_set_current_turn($game_id, 1)){
		return FALSE;
	}
	
	
	return TRUE;
}


/**
 * Perform end game action to a game.
 * 
 * set user points<br>
 * set game status.
 * 
 * @param	int		$game_id		Game ID.
 * @param	bool	$end_now		Override end round checking. (Default: FALSE)
 * 
 * @return	int|FALSE	<b>Min waterlevel</b> of game, <b>FALSE</b> if failed.
 */
function game_end_game($game_id){
	
	if(game_get_status($game_id) != GAME_STATUS_PLAYING){
		return FALSE;
	}
	
	
	db_connect();
	
	//check can be ended
	$current_round = game_get_current_round($game_id);
	$num_of_players = game_get_num_of_players($game_id);
	
	if($current_round < $num_of_players){
		return FALSE;
	}
	
	//set points
	$sql = "SELECT `user_id`, `game_points` FROM `game_has_user` WHERE `game_id` = \"$game_id\"; ";
	
	$result = mysql_query($sql);
	
	while($row = mysql_fetch_assoc($result)){
		$sql = "UPDATE `user_info` SET `user_points` = `user_points` + ${row['game_points']} WHERE `user_id` = \"${row['user_id']}\";";	
		
		
		if(!mysql_query($sql)){
			return FALSE;
		}
		
	}
	
	
	//set status
	if(!game_set_status($game_id, GAME_STATUS_ENDED)){
		return FALSE;
	}
	
	return TRUE;
}



